package cn.driver.manager.controller;

import cn.driver.common.pojo.Admin;
import cn.driver.common.tools.HttpEnum;
import cn.driver.common.tools.ResponseData;
import cn.driver.manager.service.AdService;
import cn.driver.manager.utils.Pager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vo.Adminvo;

@RestController
@RequestMapping(value = "/admin")
@CrossOrigin
@Api(tags = "admin")
public class AdminController {

    @Autowired
    private AdService adService;

    @ApiOperation("login")
    @PostMapping("/AdminLogin")
    public HttpEnum AdminLog(Adminvo adminvo){
        String name=adminvo.getAdminName();
        String password=adminvo.getPassword();
        Admin admin=adService.getAdmin(name,password);
        if (admin!=null){
            return HttpEnum.OK_200;
        }else {
        return HttpEnum.HANDLE_500;
        }
    }
    @ApiOperation("分页")
    @GetMapping("/pageInfo")
    public ResponseData PageInfo(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam(required = false) String prm){
        Pager<Admin> pagerInfo = adService.pageInfo(pageNo,pageSize,prm);
        return ResponseData.ok(pagerInfo);
    }

    @ApiOperation("添加")
    @PostMapping("/addAdmin")
    public ResponseData addAdmin(Admin admin){
        int count = adService.addAdmin(admin);
        if (count > 0){
            return ResponseData.ok("添加成功");
        }else {
            return ResponseData.error("添加失败");
        }
    }

    @ApiOperation("/修该")
    @PostMapping("/updateAdmin")
    public ResponseData updateAdmin(Admin admin){
        int count = adService.updateAdmin(admin);
        if (count >0){
            return ResponseData.ok("修该成功");
        }else {
            return ResponseData.error("修该失败");
        }
    }

    @ApiOperation("/删除")
    @PostMapping("/delAdmin")
    private ResponseData delAdmin(String account){
        int count = adService.delete(account);
        if (count>0){
            return ResponseData.ok("删除成功");
        }else {
            return ResponseData.error("删除失败");
        }
    }

}
